home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / malloc / mcheck.c < prev    next >
C/C++ Source or Header  |  1994-03-28  |  5KB  |  187 lines

  1. /* Standard debugging hooks for `malloc'.
  2.    Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation
  3.    Written May 1989 by Mike Haertel.
  4.  
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with this library; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  18. Cambridge, MA 02139, USA.
  19.  
  20.    The author may be reached (Email) at the address mike@ai.mit.edu,
  21.    or (US mail) as Mike Haertel c/o Free Software Foundation.  */
  22.  
  23. #ifndef    _MALLOC_INTERNAL
  24. #define    _MALLOC_INTERNAL
  25. #include <malloc.h>
  26. #include <stdio.h>
  27. #endif
  28.  
  29. /* Old hook values.  */
  30. static void (*old_free_hook) __P ((__ptr_t ptr));
  31. static __ptr_t (*old_malloc_hook) __P ((size_t size));
  32. static __ptr_t (*old_realloc_hook) __P ((__ptr_t ptr, size_t size));
  33.  
  34. /* Function to call when something awful happens.  */
  35. static void (*abortfunc) __P ((enum mcheck_status));
  36.  
  37. /* Arbitrary magical numbers.  */
  38. #define MAGICWORD    0xfedabeeb
  39. #define MAGICFREE    0xd8675309
  40. #define MAGICBYTE    ((char) 0xd7)
  41.  
  42. struct hdr
  43.   {
  44.     size_t size;        /* Exact size requested by user.  */
  45.     unsigned long int magic;    /* Magic number to check header integrity.  */
  46.   };
  47.  
  48. static enum mcheck_status checkhdr __P ((const struct hdr *));
  49. static enum mcheck_status
  50. checkhdr (hdr)
  51.      const struct hdr *hdr;
  52. {
  53.   enum mcheck_status status;
  54.   switch (hdr->magic)
  55.     {
  56.     default:
  57.       status = MCHECK_HEAD;
  58.       break;
  59.     case MAGICFREE:
  60.       status = MCHECK_FREE;
  61.       break;
  62.     case MAGICWORD:
  63.       if (((char *) &hdr[1])[hdr->size] != MAGICBYTE)
  64.     status = MCHECK_TAIL;
  65.       else
  66.     status = MCHECK_OK;
  67.       break;
  68.     }
  69.   if (status != MCHECK_OK)
  70.     (*abortfunc) (status);
  71.   return status;
  72. }
  73.  
  74. static void freehook __P ((__ptr_t));
  75. static void
  76. freehook (ptr)
  77.      __ptr_t ptr;
  78. {
  79.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  80.   checkhdr (hdr);
  81.   hdr->magic = MAGICFREE;
  82.   __free_hook = old_free_hook;
  83.   free (hdr);
  84.   __free_hook = freehook;
  85. }
  86.  
  87. static __ptr_t mallochook __P ((size_t));
  88. static __ptr_t
  89. mallochook (size)
  90.      size_t size;
  91. {
  92.   struct hdr *hdr;
  93.  
  94.   __malloc_hook = old_malloc_hook;
  95.   hdr = (struct hdr *) malloc (sizeof (struct hdr) + size + 1);
  96.   __malloc_hook = mallochook;
  97.   if (hdr == NULL)
  98.     return NULL;
  99.  
  100.   hdr->size = size;
  101.   hdr->magic = MAGICWORD;
  102.   ((char *) &hdr[1])[size] = MAGICBYTE;
  103.   return (__ptr_t) (hdr + 1);
  104. }
  105.  
  106. static __ptr_t reallochook __P ((__ptr_t, size_t));
  107. static __ptr_t
  108. reallochook (ptr, size)
  109.      __ptr_t ptr;
  110.      size_t size;
  111. {
  112.   struct hdr *hdr = ((struct hdr *) ptr) - 1;
  113.  
  114.   checkhdr (hdr);
  115.   __free_hook = old_free_hook;
  116.   __malloc_hook = old_malloc_hook;
  117.   __realloc_hook = old_realloc_hook;
  118.   hdr = (struct hdr *) realloc ((__ptr_t) hdr, sizeof (struct hdr) + size + 1);
  119.   __free_hook = freehook;
  120.   __malloc_hook = mallochook;
  121.   __realloc_hook = reallochook;
  122.   if (hdr == NULL)
  123.     return NULL;
  124.  
  125.   hdr->size = size;
  126.   hdr->magic = MAGICWORD;
  127.   ((char *) &hdr[1])[size] = MAGICBYTE;
  128.   return (__ptr_t) (hdr + 1);
  129. }
  130.  
  131. static void
  132. mabort (status)
  133.      enum mcheck_status status;
  134. {
  135.   const char *msg;
  136.   switch (status)
  137.     {
  138.     case MCHECK_OK:
  139.       msg = "memory is consistent, library is buggy";
  140.       break;
  141.     case MCHECK_HEAD:
  142.       msg = "memory clobbered before allocated block";
  143.       break;
  144.     case MCHECK_TAIL:
  145.       msg = "memory clobbered past end of allocated block";
  146.       break;
  147.     case MCHECK_FREE:
  148.       msg = "block freed twice";
  149.       break;
  150.     default:
  151.       msg = "bogus mcheck_status, library is buggy";
  152.       break;
  153.     }
  154.   __libc_fatal (msg);
  155. }
  156.  
  157. static int mcheck_used = 0;
  158.  
  159. int
  160. mcheck (func)
  161.      void (*func) __P ((enum mcheck_status));
  162. {
  163.   extern void abort __P ((void));
  164.  
  165.   abortfunc = (func != NULL) ? func : &mabort;
  166.  
  167.   /* These hooks may not be safely inserted if malloc is already in use.  */
  168.   if (!__malloc_initialized && !mcheck_used)
  169.     {
  170.       old_free_hook = __free_hook;
  171.       __free_hook = freehook;
  172.       old_malloc_hook = __malloc_hook;
  173.       __malloc_hook = mallochook;
  174.       old_realloc_hook = __realloc_hook;
  175.       __realloc_hook = reallochook;
  176.       mcheck_used = 1;
  177.     }
  178.  
  179.   return mcheck_used ? 0 : -1;
  180. }
  181.  
  182. enum mcheck_status
  183. mprobe (__ptr_t ptr)
  184. {
  185.   return mcheck_used ? checkhdr (ptr) : MCHECK_DISABLED;
  186. }
  187.